MovingPandas Minimum Viable Example¶
MovingPandas provides a trajectory datatype based on GeoPandas. The project home is at https://github.com/movingpandas/movingpandas
The documentation is available at https://movingpandas.readthedocs.io/en/main/
In [1]:
import warnings
warnings.filterwarnings("ignore")
In [2]:
import pandas as pd
import geopandas as gpd
import movingpandas as mpd
from hvplot import pandas
from datetime import datetime, timedelta
import folium
Loading trajectory data from a GeoPackage¶
The MovingPandas repository contains a demo GeoPackage file that can be loaded as follows:
In [3]:
gdf = gpd.read_file("../data/geolife_small.gpkg")
gdf.plot(figsize=(9, 5))
Out[3]:
<Axes: >
In [4]:
gdf.hvplot(geo=True).opts(active_tools=["pan", "wheel_zoom"])
Out[4]:
In [5]:
gdf.explore()
Out[5]:
Make this Notebook Trusted to load map: File -> Trust Notebook
After reading the trajectory point data from file, we want to construct the trajectories.
Creating a TrajectoryCollection¶
In [6]:
tc = mpd.TrajectoryCollection(gdf, "trajectory_id", t="t")
tc
Out[6]:
TrajectoryCollection with 5 trajectories
In [7]:
tc.plot(column="trajectory_id", legend=True, figsize=(9, 5))
Out[7]:
<Axes: >
In [8]:
tc.explore(column="trajectory_id", cmap="plasma", style_kwds={"weight": 4})
Out[8]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Exploring movement speed¶
In [9]:
tc.plot(
column="speed", linewidth=5, capstyle="round", legend=True, vmax=20, figsize=(9, 5)
)
Out[9]:
<Axes: >
Detecting stops¶
In [10]:
detector = mpd.TrajectoryStopDetector(tc)
stop_points = detector.get_stop_points(
min_duration=timedelta(seconds=120), max_diameter=100
)
In [11]:
ax = tc.plot(figsize=(9, 5))
stop_points.plot(ax=ax, color="red", markersize=100)
Out[11]:
<Axes: >
In [12]:
(
tc.hvplot(line_width=7, tiles=None, frame_width=400, frame_height=400)
* stop_points.hvplot(geo=True, color="black", size=100)
)
Out[12]:
In [13]:
m = tc.explore(
column="trajectory_id",
cmap="autumn",
style_kwds={"weight": 4},
name="Trajectories",
)
stop_points.explore(
m=m,
marker_kwds={"radius": 4},
name="Stop points",
)
folium.TileLayer("CartoDB dark_matter").add_to(m)
folium.LayerControl().add_to(m)
m
Out[13]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [ ]: